package com.example.nacoscloudconsumer.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.nacoscloudconsumer.entity.Article;
import com.example.nacoscloudconsumer.entity.Video;
import com.example.nacoscloudconsumer.service.EsService;
import com.example.nacoscloudconsumer.utils.Results;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class EsController {

    @Autowired
    EsService esService;

    @RequestMapping(value = "/art/getPage")
    @SentinelResource("/art/getPage")
    public Map<String, Object> getArtPage(String content, String title){
        if (("".equals(content) || "".equals(title)) || (content == null || title == null)){
            return Results.error(500, "参数错误");
        }
        List<Article> artPage = esService.getArtPage(content, title);
        if (artPage == null){
            return Results.error(404, "服务不存在");
        }
        log.info("artList:{}", artPage);
        return Results.success(artPage);
    }

    @RequestMapping(value = "/video/getPage")
    @SentinelResource("/video/getPage")
    public Object getVideoPage(String content, String title){
        if ("".equals(content) || "".equals(title)){
            return Results.error(500, "参数错误");
        }
        List<Video> videoList = esService.getVideoPage(content, title);
        if (videoList == null){
            return Results.error(404, "服务不存在");
        }
        log.info("videoList:{}", videoList);
        return Results.success(videoList);
    }
}
